//
//  LFCenterViewController.m
//  loanForYou
//
//  Created by 刘威 on 2017/8/31.
//  Copyright © 2017年 刘威. All rights reserved.
//

#import "LFCenterViewController.h"
#import "LFListViewController.h"

@interface JMDSlider : UISlider
@end

@implementation JMDSlider

- (CGRect)trackRectForBounds:(CGRect)bounds {
    return CGRectMake(0, bounds.size.height / 2.0, bounds.size.width, 3);
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return [super pointInside:point withEvent:event];
}

@end

@interface LFCenterViewController () {
    NSMutableArray *_buttonArray;
    JMDSlider *_slider;
    JMDSlider *_slider2;

    UILabel *_moneyLabel;
    UILabel *_timeLabel;

}

@property (strong, nonatomic) UIScrollView *scrollView;


@end

@implementation LFCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"借款";
    _buttonArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor grayBackgroundColor];
    // Do any additional setup after loading the view.
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.backgroundColor = [UIColor grayBackgroundColor];
    NSArray *array = @[@"上班族",@"企业主",@"其他"];
    for (NSInteger i = 0; i < array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont contentFont];
        [button setTitle:array[i]];
        [button setTitleColor:[UIColor contentColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor redButtonColor]] forState:UIControlStateSelected];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 20;
        if (i == 0) {
            button.selected = YES;
        }
        [button addTarget:self action:@selector(buttonClick:)];
        [_buttonArray addObject:button];
        [_scrollView addSubview:button];
        if (i == 1) {
            [button makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(_scrollView.centerX);
                make.top.equalTo(_scrollView.top).offset(20);
                make.size.mas_equalTo(CGSizeMake(80, 40));
            }];
        } else if (i == 0) {
            [button makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_scrollView.left).offset(20);
                make.top.equalTo(_scrollView.top).offset(20);
                make.size.mas_equalTo(CGSizeMake(80, 40));
            }];
        } else if (i == 2) {
            [button makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(_scrollView.left).offset(SCREEN_WIDTH - 20);
                make.top.equalTo(_scrollView.top).offset(20);
                make.size.mas_equalTo(CGSizeMake(80, 40));
            }];
        }
    }
    [self.view addSubview:_scrollView];
    [_scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(self.view);
    }];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.text = @"10000元";
    _moneyLabel.font = [UIFont sectionTitleFont];
    _moneyLabel.textColor = [UIColor redButtonColor];
    [_scrollView addSubview:_moneyLabel];
    [_moneyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(_scrollView.top).offset(100);
    }];
    
    
    JMDSlider *slider = [[JMDSlider alloc] initWithFrame:CGRectZero];
    _slider = slider;
    slider.minimumTrackTintColor = [UIColor contentColor];
    slider.maximumValue = 10;
    slider.minimumValue = 1;
    slider.value = slider.maximumValue;
    [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [_scrollView addSubview:slider];
    [slider makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-22);
        make.top.equalTo(_moneyLabel.bottom).offset(20);
        make.height.equalTo(@(40));
    }];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.font = [UIFont tabBarTitleFont];
    leftLabel.text = @"1000元";
    leftLabel.textColor = [UIColor grayContentColor];
    [_scrollView addSubview:leftLabel];
    [leftLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(slider.left);
        make.top.equalTo(slider.bottom);
    }];
    
    UILabel *rightLabel = [[UILabel alloc] init];
    rightLabel.font = [UIFont tabBarTitleFont];
    rightLabel.text = @"10000元";
    rightLabel.textColor = [UIColor grayContentColor];
    [_scrollView addSubview:rightLabel];
    [rightLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(slider.right);
        make.top.equalTo(slider.bottom);
    }];
    
    
    
    
    
    
    
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.text = @"12个月";
    _timeLabel.font = [UIFont sectionTitleFont];
    _timeLabel.textColor = [UIColor redButtonColor];
    [_scrollView addSubview:_timeLabel];
    [_timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(rightLabel.bottom).offset(40);
    }];
    
    JMDSlider *slider2 = [[JMDSlider alloc] initWithFrame:CGRectZero];
    slider2.minimumTrackTintColor = [UIColor contentColor];

    slider2.maximumValue = 12;
    slider2.minimumValue = 1;
    _slider2 = slider2;
    slider2.value = slider2.maximumValue;
    [slider2 addTarget:self action:@selector(sliderAction2:) forControlEvents:UIControlEventValueChanged];
    [_scrollView addSubview:slider2];
    [slider2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-22);
        make.top.equalTo(_timeLabel.bottom).offset(20);
        make.height.equalTo(@(40));
    }];
    
    UILabel *leftLabel2 = [[UILabel alloc] init];
    leftLabel2.font = [UIFont tabBarTitleFont];
    leftLabel2.text = @"1个月";
    leftLabel2.textColor = [UIColor grayContentColor];
    [_scrollView addSubview:leftLabel2];
    [leftLabel2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(slider2.left);
        make.top.equalTo(slider2.bottom);
    }];
    
    UILabel *rightLabel2 = [[UILabel alloc] init];
    rightLabel2.font = [UIFont tabBarTitleFont];
    rightLabel2.text = @"12个月";
    rightLabel2.textColor = [UIColor grayContentColor];
    [_scrollView addSubview:rightLabel2];
    [rightLabel2 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(slider2.right);
        make.top.equalTo(slider2.bottom);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"申请借款"];
    [button setBackgroundColor:[UIColor redButtonColor]];
    [button setTitleColor:[UIColor whiteColor]];
    [button addTarget:self action:@selector(next:)];
    [_scrollView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollView.left).offset(20);
        make.top.equalTo(rightLabel2.bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, 40));
    }];
}

- (void)next:(id)sender {
    LFListViewController *vc = [[LFListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)sliderAction:(UISlider *)slider {
    static NSInteger count = -1;
    NSInteger value = slider.value / 1;
    if (count != value) {
        _moneyLabel.text = [NSString stringWithFormat:@"%ld元",value * 1000] ;
    }
}

- (void)sliderAction2:(UISlider *)slider {
    static NSInteger count = -1;
    NSInteger value = slider.value / 1;
    if (count != value) {
        _timeLabel.text = [NSString stringWithFormat:@"%ld个月",(long)value] ;
    }
}

- (void)buttonClick:(UIButton *)button {
    [_buttonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = (UIButton *)obj;
        button.selected = NO;
    }];
    button.selected = YES;
    _slider.value = _slider.maximumValue;
    _moneyLabel.text = @"10000元";
    _slider2.value = _slider2.maximumValue;
    _timeLabel.text = @"12个月";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
